const { requestUrlDomain, handleTokenInvalid } = require('./request.js')
const upload = (filePath) => {
  return new Promise((resolve, reject) => {
    const inspector = wx.getStorageSync('inspector-info')
    const { token: Authorization } = JSON.parse(inspector)
    wx.uploadFile({
      url: `${requestUrlDomain}/file/upload`,
      filePath,
      name: "file",
      header: {
        Authorization
      },
      success(res) {
        const data = JSON.parse(res.data)
        if (data.code == 200) {
          resolve(data)
        } else if (data.code == 401) {
          handleTokenInvalid(data)
        } else {
          reject(data)
        }
      },
      fail(err) {
        reject(err)
      }
    })
  })
}
module.exports = upload